import operate from '@dfeidao/atom-nodejs/db/operate';
import query from '@dfeidao/atom-nodejs/db/query';
interface Message {
	role_no: string;
	productid: string;
}
interface Data {
	group_id: string;
}
export default async function atom(msg: Message, action_id: string, session_id: string) {
	const systemid = '12';
	const { del, exec } = operate(action_id, session_id, systemid);
	const { prepare: p, exec: e } = query(action_id, session_id, systemid);
	p('pub_role_allot', ['_id'], { role_no: msg.role_no, productid: msg.productid }, 199, 1, [], []);
	const [data1] = await e();
	const arr = data1.map((v: { _id: string; }) => {
		return v._id;
	});

	p('allot_conditions_group', ['group_id'], { productid: msg.productid, role_allot_id: { $in: arr } }, 199, 1, [], []);
	const [data] = await e<Data>();

	const arr1 = data.map((v) => {
		return { productid: msg.productid, group_id: v.group_id };
	});

	// data.forEach((v) => {
	arr1.forEach((v) => {
		del('allot_conditions', { productid: msg.productid, group_id: v.group_id });

	});
	// })
	// const ret1 = await exec()

	del('allot_conditions_group', { productid: msg.productid, role_allot_id: { $in: arr } });
	// const ret = await exec();

	del('pub_user_connect', { role_no: msg.role_no, productid: msg.productid });
	// const ret3 = await exec();
	// console.log(ret3)
	del('pub_role_allot', { role_no: msg.role_no, productid: msg.productid });
	const ret2 = await exec();
	if (ret2.code === 1) {
		return 'success';
	} else {
		return 'fail';
	}

}

